package com.lykj.llw.box.ui.activity

import android.os.Build
import android.os.Bundle
import android.text.Html
import android.view.View
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ViewUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.data.bean.response.GameActivityDetailResponse
import com.lykj.llw.box.databinding.ActGameActDetailBinding
import com.lykj.llw.box.utils.ImageGetterUtils
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody
import java.text.SimpleDateFormat

/**
 * ================================================
 * 游戏攻略与公告详情页
 * ================================================
 *
 * @author gosha
 * @date 2023/11/2-14:17
 */
class GameActDetailActivity : BaseActivity() {
    lateinit var binding: ActGameActDetailBinding

    private var mId = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActGameActDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        mId = intent.getIntExtra("id", 0)

        binding.titleBar.tvTitle.text = "攻略与公告"
        binding.titleBar.tvRightOption.visibility = View.GONE

    }

    private fun matchesStrNum(content: String, matches: String): Int {
        var str = content
        var count = 0
        while (str.contains(matches)) {
            str = str.substring(str.indexOf(matches) + 1)
            ++count
        }
        return count
    }

    //获取公告详情
    private fun obtainActivityDetail() {
        RetrofitManager.instance.obtainActivityDetail("$mId",
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this@GameActDetailActivity, d)
                }

                override fun specificHandle(responseBody: String) {
                    val bean =
                        GsonUtils.fromJson(responseBody, GameActivityDetailResponse::class.java)

                    binding.tvTime.text =
                        "发布时间: ${bean.data.releaseTime}"

                    if (!bean.data.content.isNullOrBlank()) {
                        val content = bean.data.content.replace("&nbsp", "").replace("&lt;", "<")
                            .replace("&gt;", ">")
                            .replace("&amp;", "&").replace("&cent;", "¢").replace("&pound;", "£")
                            .replace("&yen;", "¥").replace("&euro;", "€").replace("&sect;", "§")
                            .replace("&copy;", "©").replace("&reg;", "®").replace("&trade;", "™")

                        //判断有多少个img标签
                        var count  = matchesStrNum(content, "<img")
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            binding.tvContent.text = Html.fromHtml(
                                content,
                                Html.FROM_HTML_MODE_LEGACY,
                                ImageGetterUtils.MyImageGetter(
                                    this@GameActDetailActivity,{
                                        ViewUtils.runOnUiThreadDelayed({
                                            binding.tvContent.invalidate()

                                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                                binding.tvContent.text = Html.fromHtml(
                                                    content,
                                                    Html.FROM_HTML_MODE_LEGACY,
                                                    ImageGetterUtils.MyImageGetter(
                                                        this@GameActDetailActivity,{},0
                                                    ),
                                                    null
                                                )
                                            } else {
                                                binding.tvContent.text = Html.fromHtml(
                                                    content,
                                                    ImageGetterUtils.MyImageGetter(
                                                        this@GameActDetailActivity,{},0
                                                    ),
                                                    null
                                                )
                                            }
                                        }, 5)
                                    },count
                                ),
                                null
                            )
                        } else {
                            binding.tvContent.text = Html.fromHtml(
                                content,
                                ImageGetterUtils.MyImageGetter(
                                    this@GameActDetailActivity,{
                                        ViewUtils.runOnUiThreadDelayed({
                                            binding.tvContent.invalidate()

                                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                                binding.tvContent.text = Html.fromHtml(
                                                    content,
                                                    Html.FROM_HTML_MODE_LEGACY,
                                                    ImageGetterUtils.MyImageGetter(
                                                        this@GameActDetailActivity,{},0
                                                    ),
                                                    null
                                                )
                                            } else {
                                                binding.tvContent.text = Html.fromHtml(
                                                    content,
                                                    ImageGetterUtils.MyImageGetter(
                                                        this@GameActDetailActivity,{},0
                                                    ),
                                                    null
                                                )
                                            }
                                        }, 5)
                                    },count
                                ),
                                null
                            )
                        }
                    }
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@GameActDetailActivity, it)
                    }
                }
            })
    }

    override fun initData() {
        obtainActivityDetail()
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}